<?php

	/***********************************************\
	 * N2F Yverdon v0                              *
	 * Copyright (c) 2009 Zibings Incorporated     *
	 *                                             *
	 * You should have received a copy of the      *
	 * Microsoft Reciprocal License along with     *
	 * this program.  If not, see:                 *
	 * <http://opensource.org/licenses/ms-rl.html> *
	\***********************************************/

	/*
	 * $Id: user.ext.php 113 2010-10-16 04:25:14Z amale $
	 */

	// Pull in our globals
	global $user, $strings;
	$n2f = n2f_cls::getInstance();

	// Register the extension
	$n2f->registerExtension(
		'user',
		'n2f_user',
		0.2,
		'Andrew Male',
		'http://n2framework.com/'
	);

	// Hook the N2F_EVT_CORE_LOADED event
	$n2f->hookEvent(N2F_EVT_CORE_LOADED, 'init_user');

	// Create the user extension warning constant
	define('N2F_WARN_USER_TIMEOUT',				'U001');

	// Create the user extension notice constant
	define('N2F_NOTICE_USER_INIT',				'U001');

	// English warning strings
	$strings['en']['N2F_WARN_USER_TIMEOUT']			= "The user has timed out.";

	// German warning strings
	$strings['de']['N2F_WARN_USER_TIMEOUT']			= "The user has timed out.";

	// Spanish warning strings
	$strings['es']['N2F_WARN_USER_TIMEOUT']			= "The user has timed out.";

	// Swedish warning strings
	$strings['se']['N2F_WARN_USER_TIMEOUT']			= "The user has timed out.";

	// English notice strings
	$strings['en']['N2F_NOTICE_USER_INIT']			= "The user object has been initialized.";

	// German notice strings
	$strings['de']['N2F_NOTICE_USER_INIT']			= "The user object has been initialized.";

	// Spanish notice strings
	$strings['es']['N2F_NOTICE_USER_INIT']			= "The user object has been initialized.";

	// Swedish notice strings
	$strings['se']['N2F_NOTICE_USER_INIT']			= "The user object has been initialized.";

	/**
	 * Function that tries to initialize the $user global.
	 *
	 * @param n2f_cls $n2f		The object who is calling this function
	 * @param mixed $results		The results returned from the system loading its core
	 * @return null
	 */
	function init_user(n2f_cls &$n2f, $results) {
		// Check if there was a massive failure or if the session extension isn't loaded
		if ($results === false || $n2f->hasExtension('session') === false) {
			// And if either is the case, just stop here
			return(null);
		}

		// Pull in global variable(s)
		global $user, $sess;

		// Initialize the timeout stamp
		$timeout = (time() - 300);

		// Check if there is a session user
		if ($sess->exists('n2f_sess_user')) {
			// There is, so pull them out
			$user = $sess->get('n2f_sess_user');

			// Just check if they've timed out
			if ($user->lasttime < $timeout) {
				// And if so, reset their properties
				$user->user_id = 0;
				$user->lasttime = time();

				// If we're supposed to track warnings..
				if ($n2f->debug->showLevel(N2F_DEBUG_WARN)) {
					// Throw a warning to the main debug object
					$n2f->debug->throwWarning(N2F_WARN_USER_TIMEOUT, S('N2F_WARN_USER_TIMEOUT'), 'system/extensions/user.ext.php');
				}
			}
		} else {
			// Otherwise, initialize a new user and add them to the session
			$user = new n2f_user();
			$sess->set('n2f_sess_user', $user);
		}

		// If we're supposed to track notices..
		if ($n2f->debug->showLevel(N2F_DEBUG_NOTICE)) {
			// Throw a notice to the main debug object
			$n2f->debug->throwNotice(N2F_NOTICE_USER_INIT, S('N2F_NOTICE_USER_INIT'), 'system/extensions/user.ext.php');
		}

		// And stop processing, we've got nothing left to do!
		return(null);
	}

	/**
	 * Example n2f_user class.
	 *
	 */
	class n2f_user {
		/**
		 * User identifier property.
		 *
		 * @var integer
		 */
		public $user_id;
		/**
		 * Last timestamp when user was active.
		 *
		 * @var integer
		 */
		public $lasttime;

		/**
		 * Initializes a new n2f_user object.
		 *
		 * @return n2f_user
		 */
		public function __construct() {
			// Initialize the properties
			$this->user_id = 0;
			$this->lasttime = time();

			// Return ourself for chaining
			return($this);
		}
	}

?>